/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameWork.Objects.Character;

import FrameWork.Audio.RSAudio;
import FrameWork.GameScreen;
import FrameWork.Objects.Items.boom;
import FrameWork.Objects.MyObject;
import GamePlay.Global;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author ASUS
 */
public class Player extends MyCharacter{
    
    public  int       m_HP;       // Hp fire man
    public  int       m_NumWater; // Number of water fireman colleced
    public  int       m_MaxWater;
    public  boolean   m_iKey;     // is Key of fireman collec  
    public  boolean   m_iGo;
    public  boolean   m_iKick;
    public  Vector    m_vBoom;
    public  int       m_LenghtBoom;
    public  int       m_MaxBoom;
    
    public Player (int _X,int _Y, int _SizeX, int _SizeY,
            float _TimeAni, float _TimeSpeed,int _Direct,
            int _Map[][],int _Width, int _Height, int _ID) throws IOException{        
        super(_X,_Y,_SizeX,_SizeY, _TimeAni, _TimeSpeed,
                _Direct,_Map,_Width,_Height, _ID);
        
        m_NumWater  = 5;
        m_HP        = 4;
        m_iKey      = false;
        m_iGo       = false;
        m_vBoom     = new Vector();
        m_LenghtBoom= 1;
        m_MaxBoom   = 1;
        m_iKick     = false;
        m_CurFrame  = 24;
        m_MaxWater  = 10;
    }
    public int getHP (){
        return m_HP;
    }
    public boolean  getKey (){
        return m_iKey;
    }
    public void setKey (boolean _Key){
        m_iKey =_Key;
    }
    public void setLenghtBoom (int _Lenght){
        m_LenghtBoom = _Lenght;
    }
    public int getLenghtBoom (){
        return m_LenghtBoom;
    }
    public void setKick (boolean _Kick){
        m_iKick = _Kick;
    }
    public boolean getKick (){
        return m_iKick;
    }
    public void UpdateAnimation (float _Time){
        
        m_SaveTimeAni += _Time;
        if (m_SaveTimeAni>= m_TimeAni){
            m_SaveTimeAni = 0;
            switch (m_CurDirect)
            {
            case Global.DIR_UP: 
                m_CurFrame= (m_CurFrame + 1)%3 + 33;
                break;
            case Global.DIR_DOWN: 
                m_CurFrame= (m_CurFrame+1)%3 + 24;
                break;
            case Global.DIR_LEFT:
                m_CurFrame= (m_CurFrame+1)%3 + 27;
                break;
            case Global.DIR_RIGHT: 
                m_CurFrame= (m_CurFrame+1)%3 + 30;
                break;
            }        
        }
        
    }
    
    public void ActionCollision (MyObject _Object){
        
        if ( _Object.getSTATUS()==Global.STT_DEATH)return;
        switch (_Object.getID()){
            //<editor-fold defaultstate="collapsed" desc="Water">
            case Global.ID_WATER:
                if(m_MaxWater-m_NumWater >=5){
                    m_NumWater+=5;
                    _Object.setSTATUS(Global.STT_DEATH);
                    RSAudio.getRS().PlayBubble();
                }
                break;
                //</editor-fold>
                
            //<editor-fold defaultstate="collapsed" desc="Fire">
            case Global.ID_FIRE:
                    if(m_STATUS==Global.STT_ACTIVE){
                        m_STATUS= Global.STT_INVI;
                        m_HP--;
                    }
                break;
                //</editor-fold>
                
            //<editor-fold defaultstate="collapsed" desc="Monster">
            case Global.ID_HOUSE:
            case Global.ID_PIG:
            case Global.ID_CHICKEN:
            case Global.ID_PIGAI:
            case Global.ID_CHICKENAI:
            case Global.ID_FRIEND:
                if(m_STATUS==Global.STT_ACTIVE &&
                        _Object.getSTATUS()== Global.STT_ACTIVE){
                    m_STATUS= Global.STT_INVI;
                    m_HP--;
                }
                break;
                //</editor-fold>
                
            //<editor-fold defaultstate="collapsed" desc="KEY">
            case Global.ID_KEY:
                if(m_iKey==false){
                    m_iKey=true;
                     _Object.setSTATUS(Global.STT_DEATH);
                     RSAudio.getRS().PlayBubble();
                }
                break;
            //</editor-fold>
           
            //<editor-fold defaultstate="collapsed" desc="Kick">
            case Global.ID_KICK:
                    m_iKick = true;
                    _Object.setSTATUS(Global.STT_DEATH);
                    RSAudio.getRS().PlayBubble();
                break;
            //</editor-fold>
                
            //<editor-fold defaultstate="collapsed" desc="Add boom">
            case Global.ID_ADD_BOOM:
                m_MaxBoom++;
                _Object.setSTATUS(Global.STT_DEATH);
                RSAudio.getRS().PlayBubble();
                break;
            //</editor-fold>
                
            //<editor-fold defaultstate="collapsed" desc="Add lenght boom">
            case Global.ID_ADD_LENGHTBOOM:
                
                m_LenghtBoom++;
                if(m_LenghtBoom>4){
                    m_LenghtBoom = 4;
                }
                _Object.setSTATUS(Global.STT_DEATH);
                RSAudio.getRS().PlayBubble();
                break;
            //</editor-fold>   
                
            //<editor-fold defaultstate="collapsed" desc="Add max water">
            case Global.ID_ADD_MAXWATER:
                m_MaxWater+=5;
                _Object.setSTATUS(Global.STT_DEATH);
                RSAudio.getRS().PlayBubble();
                break;
            //</editor-fold> 
                
            //<editor-fold defaultstate="collapsed" desc="Add heath">
            case Global.ID_ADD_HP:
                m_HP++;
                _Object.setSTATUS(Global.STT_DEATH);
                RSAudio.getRS().PlayBubble();
                break;
            //</editor-fold> 
                
            //<editor-fold defaultstate="collapsed" desc="Add Speed">
            case Global.ID_ADD_SPEED:
                m_Veloc+=25;
                _Object.setSTATUS(Global.STT_DEATH);
                RSAudio.getRS().PlayBubble();
                break;
            //</editor-fold> 
                
            //<editor-fold defaultstate="collapsed" desc="Boom Explotion">
            case Global.ID_BOOM_EXPLOSION:
               if(m_STATUS==Global.STT_ACTIVE){
                        m_STATUS= Global.STT_INVI;
                        m_HP--;
                    }
                break;
            //</editor-fold> 
                
        }
    }
    public void ActiveItems (Vector _Items){
        int _X = m_X;
        int _Y = m_Y;
        int NextX = _X;
        int NextY = _Y;
        switch(m_CurDirect){
            case Global.DIR_LEFT:
                if(_X >= 1){
                    NextX = _X-1;
                }
                break;
            case Global.DIR_RIGHT:
                if(_X <m_WidthMap-1){
                    NextX = _X+1;
                }
                break;
            case Global.DIR_UP:
                if(_Y>=1) {
                    NextY = _Y-1;
                }
                break;
            case Global.DIR_DOWN:
                if(_Y < m_HeightMap-1){
                    NextY = _Y+1;
                }
                break;
        }
        for (int i = 0; i < _Items.size(); i++) {
            MyObject object = (MyObject)_Items.elementAt(i);
                    if(NextX == object.getX() && NextY==object.getY()){
                    object.Active(this);
                }
            
        }
    }
    public void Update (float _Time,int _Terrain[][]){
        UpdateListBoom();
        UpdateMove(_Time, _Terrain);
        UpdateAnimation(_Time);
        Move(_Time, _Terrain);
        switch(m_STATUS){
            case Global.STT_INVI:
                m_TimeUpdate+=_Time;
//                if(m_TimeUpdate>=1.5f){
//                    m_TimeUpdate = 0;
                    if(m_HP<=0){
                        if(m_TimeUpdate>=0.5f){
                            m_TimeUpdate = 0;
                            m_STATUS= Global.STT_DEATH;   
                        }
                    }else{
                        if(m_TimeUpdate>=1.5f){
                            m_TimeUpdate = 0;
                            m_STATUS= Global.STT_ACTIVE; 
                        }
                    }
                                       
//                }
                break;
        }
    }
    public void DrawScreen (Graphics _Graphics,GameScreen _Screen){
          
        switch(m_STATUS){
            case Global.STT_ACTIVE:
                super.DrawScreen(_Graphics, _Screen);
                break;
            case Global.STT_INVI:
                if (m_Ran.nextInt(2)==0){
                   super.DrawScreen(_Graphics, _Screen);
                }
                break;
        }
        
    }
    public void setDirection (int _Dir){
        if(!m_iRun){
           m_Direct =_Dir; 
           m_iGo  = true;
        }
    }
    public void Move(float _Time,int _Terrain[][]){
        if(!m_iRun){
             if(m_iGo==true){
                m_NextX = m_X;
                m_NextY = m_Y;
                FindNextStep(m_X, m_Y, m_Direct);         
               if(m_X!=m_NextX || m_Y!= m_NextY){
                   if(_Terrain[m_NextX][m_NextY]!=Global.ID_GRASS &&
                        _Terrain[m_NextX][m_NextY]!=Global.ID_BOOM &&
                        _Terrain[m_NextX][m_NextY]!=Global.ID_DOOR &&
                        _Terrain[m_NextX][m_NextY]!=Global.ID_HOUSE){
                      
                       m_CurDirect = m_Direct;
                       m_iRun = true;
                      
                   }
                   else{
                       m_CurDirect = m_Direct;
                   }
               }else{
                   m_CurDirect = m_Direct;
               }
               m_iGo = false;
            }
       }else{
           MoveLayout(_Time);
       }        
    }
    public void AddBoom (Vector _ListItem){
       
        if (m_vBoom.size()<m_MaxBoom) {
            for (int i = 0; i < m_vBoom.size(); i++) {
                boom bt =(boom) m_vBoom.elementAt(i);
                if (bt.getX() == getX() && bt.getY() == getY()){
                    return;
                }
            }
            if(m_NumWater<=0){
                    return;
            }
            m_NumWater--;
            boom b = new boom(_ListItem,m_LenghtBoom, m_X, m_Y, m_SizeX, m_SizeY, m_TimeAni,
                m_Map, m_WidthMap, m_HeightMap, Global.ID_BOOM);
            _ListItem.addElement(b);
            m_vBoom.addElement(b);
        }
        
    }
    public void UpdateListBoom (){
        for (int i = 0; i < m_vBoom.size();) {
                boom bt =(boom) m_vBoom.elementAt(i);
                if (bt.getOldStatus()== Global.STT_DEATH && bt.getSTATUS()== Global.STT_DEATH){
                    m_vBoom.removeElementAt(i);
                }else i++;
            }
    }
}
